package com.fish.common.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;

/**
 * 邮件发送服务
 *
 * @author fish
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EmailService {

    private final JavaMailSender mailSender;

    /**
     * 发送简单文本邮件
     */
    public void sendSimpleMail(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("3401611059@qq.com");
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            mailSender.send(message);
            log.info("发送简单邮件成功，收件人：{}", to);
        } catch (Exception e) {
            log.error("发送简单邮件失败，收件人：{}", to, e);
            throw new RuntimeException("邮件发送失败：" + e.getMessage());
        }
    }

    /**
     * 发送HTML邮件
     */
    public void sendHtmlMail(String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom("3401611059@qq.com");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            log.info("发送HTML邮件成功，收件人：{}", to);
        } catch (MessagingException e) {
            log.error("发送HTML邮件失败，收件人：{}", to, e);
            throw new RuntimeException("邮件发送失败：" + e.getMessage());
        }
    }

    /**
     * 发送验证码邮件
     */
    public void sendVerificationCode(String to, String code) {
        String subject = "Fish System - 邮箱验证码";
        String content = String.format(
                "<html>" +
                "<body style='font-family: Arial, sans-serif;'>" +
                "<div style='max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f5f5f5;'>" +
                "<div style='background-color: white; padding: 30px; border-radius: 10px;'>" +
                "<h2 style='color: #409EFF; text-align: center;'>邮箱验证码</h2>" +
                "<p style='font-size: 16px; color: #333;'>您好！</p>" +
                "<p style='font-size: 14px; color: #666;'>您正在进行邮箱验证操作，验证码为：</p>" +
                "<div style='text-align: center; margin: 30px 0;'>" +
                "<span style='font-size: 32px; font-weight: bold; color: #409EFF; letter-spacing: 5px;'>%s</span>" +
                "</div>" +
                "<p style='font-size: 14px; color: #666;'>验证码有效期为5分钟，请尽快使用。</p>" +
                "<p style='font-size: 14px; color: #999; margin-top: 30px;'>如非本人操作，请忽略此邮件。</p>" +
                "</div>" +
                "<p style='text-align: center; font-size: 12px; color: #999; margin-top: 20px;'>" +
                "Fish System © 2025" +
                "</p>" +
                "</div>" +
                "</body>" +
                "</html>",
                code
        );
        sendHtmlMail(to, subject, content);
    }
}
